package com.qing.microservices.shirostudy.controller;

import com.qing.microservices.shirostudy.entity.Authority;
import com.qing.microservices.shirostudy.service.IAuthorityService;
import com.qing.microservices.shirostudy.vo.AuthorityVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Q
 * @since 2023-06-06
 */
@Slf4j
@RestController
@RequestMapping("/authority")
public class AuthorityController {

    @Resource
    private IAuthorityService authorityService;

    @PostMapping(value = "/addition")
    public void addAuthority(@RequestBody List<AuthorityVO> authorityVOList) {
        List<Authority> authorityList = authorityVOList.stream().map(auth -> {
            Authority authority = new Authority();
            authority.setName(auth.getName());
            authority.setUrl(auth.getUrl());
            return authority;
        }).collect(Collectors.toList());

        boolean saved = authorityService.saveBatch(authorityList);
        log.info("保存权限[{}]", saved ? "成功" : "失败");
    }
}
